#coding:utf-8

import json

from mysql_store import initMySqlStore
from es_store import initEsDocumentStore

def getLabel(location):
    store = initMySqlStore()
    rs = store.getAll()
    cnt = 0
    zs = ['title1','title2','content1','content2']
    with open(location,mode='w') as f:
        for r in rs:
            for z in zs:
                v = r[z]
                if isinstance(v,str):
                    v = v.decode('utf-8')
                    r[z] = v

            cnt += 1
            if cnt % 1000 == 0:
                print cnt
            line = json.dumps(r,ensure_ascii=False)
            f.write(line.encode('utf-8')+'\n')

def isExist(es,i1,i2):
    try:
        r1 = es.get(i1,fields=['term_score','cut_title','cut_content'])
        r2 = es.get(i2,fields=['term_score','cut_title','cut_content'])
        return True
    except:
        return False


def getCase(location1,location2):
    es = initEsDocumentStore()
    f1 = open(location1,mode='r')
    f2 = open(location2,mode='w')

    for line in f1:
        r = json.loads(line)
        i1 = r['item_id1']
        i2 = r['item_id2']
        if (r['status'] == 3 and r['source'] == 0) or (r['status'] == 3 and r['source'] == 1): # 相似
            if not isExist(es,i1,i2):
                continue
            line = '%s,%s,%s' % (r['item_id1'],r['item_id2'],1)
            f2.write(line.encode('utf-8')+'\n')
        if (r['status'] == 1 and r['source'] == 2): # 不相似
            if not isExist(es,i1,i2):
                continue
            line = '%s,%s,%s' % (r['item_id1'],r['item_id2'],0)
            f2.write(line.encode('utf-8')+'\n')

    f1.close()
    f2.close()

def getData(location1,location2):
    es = initEsDocumentStore()
    f1 = open(location1,mode='r')
    f2 = open(location2,mode='w')
    
    cnt = 0
    for line in f1:
        #m = json.loads(line)
        #i1 = m['item_id1']
        #i2 = m['item_id2']
        i1,i2,z = line.strip().split(',')
        try:
            r1 = es.get(i1,fields=['term_score','cut_title','cut_content','title'])
            r2 = es.get(i2,fields=['term_score','cut_title','cut_content','title'])

            vs1 = r1['term_score']
            vs2 = r2['term_score']
            vs1 = json.loads(vs1)
            vs2 = json.loads(vs2)
            ct1 = r1['cut_title']
            cc1 = r1['cut_content']
            t1 = r1['title']
            ct2 = r2['cut_title']
            cc2 = r2['cut_content']
            t2 = r2['title']
            cnt += 1
            print i1,i2
        except Exception,e:
            #print i1,i2
            continue
        n = {}
        n['i1'] = i1
        n['i2'] = i2
        n['v1'] = vs1
        n['v2'] = vs2
        #n['s'] = m['status']
        #n['e'] = m['source']
        n['ct1'] = ct1
        n['cc1'] = cc1
        n['ct2'] = ct2
        n['cc2'] = cc2

        n['t1'] = t1
        n['t2'] = t2
        n['z'] = z
        line = json.dumps(n,ensure_ascii=False)
        f2.write(line.encode('utf-8')+'\n')
        

    f1.close()
    f2.close()
    print cnt


if __name__ == '__main__':
    
    import sys
    i = sys.argv[1]

    if i == 'z':
        location = 'data/issue.txt'
        getLabel(location)
    
    if i == 'u':
        location1 = 'data/issue.txt'
        location2 = 'data/issue.u.txt'
        getCase(location1,location2)

    if i == 'v':
        location1 = 'data/issue.u.txt'
        location2 = 'data/issue.v.txt'
        getData(location1,location2)


